<html>
<head>
<title>Results of Query</title>
<body>

<h2>Results of Query</h2>
<hr>
<br>

<?php

//Connect to MySQL

  $connection = mysql_connect( "127.0.0.1", "root", "abc123" );
  
  if ( !$connection )
  {
    die("Could not connect: " . mysql_error() );    
  }

  mysql_select_db( "sasystemTables", $connection );


//Check for variables from previous page

  if ( array_key_exists( "hasCar", $_GET ) )
  { 
    $hasCar = "True";
  }
  else
  {
    $hasCar = "False";
  }

  if ( array_key_exists( "distance", $_GET ) )
  { 
    $distance = $_GET['distance'];
  }
  else
  {
    $distance = NULL;
  }

  if ( array_key_exists( "class", $_GET ) )
  { 
    $class = "class";
  }
  else
  {
    $class = NULL;
  }

  if ( array_key_exists( "prevExp", $_GET ) )
  { 
    $prevExp = $_GET['prevExp'];
  }
  else
  {
    $prevExp = NULL;
  }

  if ( array_key_exists( "prevTeacher", $_GET ) )
  { 
    $prevTeacher = $_GET['prevTeacher'];
  }
  else
  {
    $prevTeacher = NULL;
  }


//Structure the query depending on what was passed in

  $query = "SELECT  L1.userID " .
           "FROM    Logins AS L1 NATURAL JOIN Travel AS T2 " .
           "WHERE   L1.role = 'student' ;";

  $result = mysql_query( $query, $connection );

  if ( $hasCar != "False" )
  {
    $query2 = "SELECT  L1.userID " .
              "FROM    Logins AS L1 NATURAL JOIN Travel AS T2 " .
              "WHERE   L1.role = 'student' " .
              "AND     T2.hasCar = '$hasCar' ;";

    $result = mysql_query( $query2, $connection );

  }

  if ( $distance != NULL )
  {
    $query3 = "SELECT  L1.userID " .
              "FROM    Logins AS L1 NATURAL JOIN Schools AS S2 " .
              "WHERE   L1.role = 'student' " .
              "AND     S2.distance > '$distance' ;";

    $result = mysql_query( $query3, $connection );

  }

  if ( $class != NULL )
  {
    $query4 = "SELECT  L1.userID " .
              "FROM    Logins AS L1 NATURAL JOIN Conflicts AS C2 " .
              "WHERE   L1.role = 'student' " .
              "AND     C2.conflictType = '$class' ;";

    $result = mysql_query( $query4, $connection );

  }

  if ( $prevExp != NULL )
  {
    $query5 = "SELECT  L1.userID " .
              "FROM    Logins AS L1 NATURAL JOIN FieldExperience AS F2 " .
              "WHERE   L1.role = 'student' " .
              "AND     F2.school = '$prevExp' ;";

    $result = mysql_query( $query5, $connection );

  }

  if ( $prevTeacher != NULL )
  {
    $query6 = "SELECT  L1.userID " .
              "FROM    Logins AS L1 NATURAL JOIN FieldExperience AS F2 " .
              "WHERE   L1.role = 'student' " .
              "AND     F2.mentor = '$prevTeacher' ;";

    $result = mysql_query( $query6, $connection );

  }

//Create the page table with the retrieved data

  echo "<table border = '1' >" . "<tr>" . "<th>Student</th>" . "<th>Teacher</th>" . "</tr>\n";

  $studentAndTeacher = array(); 

  while( $row = mysql_fetch_array( $result ) )
  {
    echo "<tr><td>" . $row['userID'] . "</td>";
    echo "<td><input type=\"text\" name=\"teacher\" ></td></tr>";
    array_push( $studentAndTeacher, $row['userID'] );
  }

  echo "</table>\n";
  

//Close the connection

  mysql_close( $connection );

?>

<br><br>
<a href="startmatching.php"><input type="button" value="Go back to matching screen"></a>
<?php echo "<a href = \"match_result.php?id=\'$studentAndTeacher\'\">"?>
<input type="button" value="Pair Students"></a>

</body>
</html>
